import {
	requestURL
} from './config'
import {
	_showModal
} from '../js/util.js'

function getType(data, method) { // 获取请求方式
	let type = {}
	if (method == 'post') {
		method = 'post'
		type = {
			method,
			// data: typeof data == 'string'?data:{...data},
			data,
			header: {
				'content-type': 'application/json'
			}
		}
	} else {
		method = 'get'
		type = {
			method,
			data
		}
	}
	return type
}

function request(url, method, data, header = {
	'Content-Type': 'application/json'
}) {
	let cookie = uni.getStorageSync('ZJTOKEN');
	let tokenDate = uni.getStorageSync('ZJTOKENDATE');
	if (tokenDate) {
		if (new Date().getTime()>new Date(tokenDate).getTime()) {
			_showModal('温馨提示', '登录过期，请重新登录')
			uni.removeStorageSync("ZJTOKENDATE")
			uni.removeStorageSync("ZJTOKEN")
			uni.removeStorageSync("s_userId")
			uni.removeStorageSync("s_empId")
			uni.removeStorageSync("s_login")
			uni.removeStorageSync("s_mobile")
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}
	}
	if (cookie) {
		header.Cookie = cookie;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: requestURL + url,
			...getType(data, method),
			header,
			success: res => {
				console.log(res)
				if (res && res.header && res.header['Set-Cookie'] && res.header[
						'Set-Cookie'].indexOf('ecmToken') != -1) {
					const xjtoken = res.header['Set-Cookie']
					const date = xjtoken.split(';').filter(i => i.indexOf(
						'GMT') != -1)[0] //获取过期时间
					uni.setStorageSync('ZJTOKENDATE', date);
					uni.setStorageSync('ZJTOKEN', res.header[
						'Set-Cookie']); //保存Cookie到Storage
				}
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					if (res.statusCode == 500) {
						_showModal('温馨提示', '服务器端出错, 请稍后打开!')
					} else if (res.statusCode == 502) {
						_showModal('温馨提示', '网络超时,请稍后打开!')
					} else if (res.statusCode == 404) {
						_showModal('温馨提示', '请求错误, 未找到该资源!')
					} else {
						_showModal('温馨提示', res.errMsg)
					}
				}
			},
			fail: err => {
				uni.hideLoading()
				_showModal('温馨提示', '网络超时,请稍后打开!')
				reject(err)
			},
			complete: function() {
				// uni.hideLoading()
			}
		})
	}).catch(err => {
		uni.hideLoading()
		reject(err)
	})
}
// 定向请求
function orientationRequest(domain, url, method, data, header = {
	'Content-Type': 'application/json'
}) {

	let cookie = uni.getStorageSync('ZJTOKEN');
	let tokenDate = uni.getStorageSync('ZJTOKENDATE');
	if (tokenDate) {
		if (new Date().getTime()>new Date(tokenDate).getTime()) {
			_showModal('温馨提示', '登录过期，请重新登录')
			uni.removeStorageSync("ZJTOKENDATE")
			uni.removeStorageSync("ZJTOKEN")
			uni.removeStorageSync("s_userId")
			uni.removeStorageSync("s_empId")
			uni.removeStorageSync("s_login")
			uni.removeStorageSync("s_mobile")
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}
	}
	if (cookie) {
		header.Cookie = cookie;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: domain + url,
			...getType(data, method),
			header,
			success: res => {
				if (res && res.header && res.header['Set-Cookie'] && res.header[
						'Set-Cookie'].indexOf('ecmToken') != -1) {
					const xjtoken = res.header['Set-Cookie']
					const date = xjtoken.split(';').filter(i => i.indexOf(
						'GMT') != -1)[0] //获取过期时间
					uni.setStorageSync('ZJTOKENDATE', date);
					uni.setStorageSync('ZJTOKEN', res.header[
						'Set-Cookie']); //保存Cookie到Storage
				}
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					if (res.statusCode == 500) {
						_showModal('温馨提示', '服务器端出错, 请稍后打开!')
					} else if (res.statusCode == 502) {
						_showModal('温馨提示', '网络超时,请稍后打开!')
					} else if (res.statusCode == 404) {
						_showModal('温馨提示', '请求错误, 未找到该资源!')
					} else {
						_showModal('温馨提示', res.errMsg)
					}
				}
			},
			fail: err => {
				uni.hideLoading()
				_showModal('温馨提示', '网络超时,请稍后打开!')
				reject(err)
			},
			complete: function() {
				// uni.hideLoading()
			}
		})
	}).catch(err => {
		uni.hideLoading()
		reject(err)
	})
}

export {
	request,
	orientationRequest
}